package com.workcell.workdata.im.component.listener;

import com.workcell.workdata.im.component.event.DeleteMessageEvent;
import com.workcell.workdata.im.component.event.MomentEvent;
import com.workcell.workdata.im.constant.ChangeType;
import com.workcell.workdata.im.constant.FileBucket;
import com.workcell.workdata.im.constant.MessageFormat;
import com.workcell.workdata.im.entity.Message;
import com.workcell.workdata.im.entity.Moment;
import com.workcell.workdata.im.model.message.*;
import com.workcell.workdata.im.service.FileStoreService;
import com.workcell.workdata.im.util.JSON;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Component
public class DiskFileRemover {

    @Resource
    private FileStoreService fileStoreService;

    @EventListener
    public void onDeleteMessageEvent(DeleteMessageEvent event){
        Message message = event.getSource();
        if (MessageFormat.FILE == (message.getFormat())) {
            ChatFile chatFile = JSON.parse(message.getContent(), ChatFile.class);
            fileStoreService.delete(FileBucket.CHAT_SPACE.getValue(),chatFile.getFile());
        }
        if (MessageFormat.MAP == (message.getFormat())) {
            ChatMap chatMap = JSON.parse(message.getContent(), ChatMap.class);
            fileStoreService.delete(FileBucket.CHAT_SPACE.getValue(),chatMap.getImage());
        }
        if (MessageFormat.VOICE == (message.getFormat())) {
            ChatVoice chatVoice = JSON.parse(message.getContent(), ChatVoice.class);
            fileStoreService.delete(FileBucket.CHAT_SPACE.getValue(),chatVoice.getFile());
        }
        if (MessageFormat.IMAGE == (message.getFormat())) {
            CloudImage image = JSON.parse(message.getContent(), CloudImage.class);
            fileStoreService.delete(image.getBucket(),image.getImage());
            fileStoreService.delete(image.getBucket(),image.getThumb());
        }
        if (MessageFormat.VIDEO == (message.getFormat())) {
            CloudVideo video = JSON.parse(message.getContent(), CloudVideo.class);
            fileStoreService.delete(video.getBucket(),video.getVideo());
            fileStoreService.delete(video.getBucket(),video.getImage());
        }
    }

    @EventListener
    public void onDeleteMomentEvent(MomentEvent event){

        if (event.getType() != ChangeType.DELETE){
            return;
        }

        Moment moment = event.getSource();

        if (Objects.equals(moment.getType(), Moment.FORMAT_IMAGE)) {
            CloudImage image = JSON.parse(moment.getContent(), CloudImage.class);
            fileStoreService.delete(FileBucket.MOMENT_SPACE.getValue(),image.getImage());
            fileStoreService.delete(FileBucket.MOMENT_SPACE.getValue(),image.getThumb());
        }

        if (Objects.equals(moment.getType(), Moment.FORMAT_MULTI_IMAGE)) {

            List<CloudImage> snsImageList = JSON.parseList(moment.getContent(), CloudImage.class);
            snsImageList.forEach(image -> {
                fileStoreService.delete(FileBucket.MOMENT_SPACE.getValue(),image.getImage());
                fileStoreService.delete(FileBucket.MOMENT_SPACE.getValue(),image.getThumb());
            });
        }

        if (Objects.equals(moment.getType(), Moment.FORMAT_VIDEO)) {
            CloudVideo video = JSON.parse(moment.getContent(), CloudVideo.class);
            fileStoreService.delete(FileBucket.MOMENT_SPACE.getValue(),video.getVideo());
            fileStoreService.delete(FileBucket.MOMENT_SPACE.getValue(),video.getImage());
        }
    }

}
